#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建3行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    //创建3列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    //给3个列设置列名(设置水平方向的表头)
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("年龄"));

    //给表格中添加数据
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("20"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("1002"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("19"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("1003"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("20"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insertRow_clicked()
{
    //需要知道当前一共有多少行
    int rowCount = ui->tableWidget->rowCount();
    //在最后一行之后新增新行
    //注意:此处的参数表示新增之后这一行是第几行(即下标),正好是总行数的个数-->eg:现有3行,在最后一行之后新增一行,新增的行数从第0行开始数,就是第三行
    ui->tableWidget->insertRow(rowCount);
}


void Widget::on_pushButton_deleteRow_clicked()
{
    //获取到当前选中的行号
    int curRow = ui->tableWidget->currentRow();
    //删除这一行
    ui->tableWidget->removeRow(curRow);
}

void Widget::on_pushButton_insertColumn_clicked()
{
    //需要知道当前一共有多少列
    int columnCount = ui->tableWidget->columnCount();
    //在最后一列之后新增新列
    //注意:此处的参数表示新增之后这一列是第几列(即下标),正好是总列数的个数-->eg:现有3列,在最后一列之后新增一列,新增的列数从第0列开始数,就是第三列
    ui->tableWidget->insertColumn(columnCount);
    //设置新增列的列名(从输入框中获取到)
    const QString& text = ui->lineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(columnCount,new QTableWidgetItem(text));
}

void Widget::on_pushButton_deleteColumn_clicked()
{
    //获取到当前选中的列号
    int curColumn = ui->tableWidget->currentColumn();
    //删除这一列
    ui->tableWidget->removeColumn(curColumn);
}
